package com.ruoyi.common.utils;

import cn.hutool.core.lang.Assert;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;

/**
 * 使用同一个签名，对同一个手机号码发送短信验证码，支持1条/分钟，5条/小时，10条/天。
 * 一个手机号码通过阿里云平台只能收到40条/天。
 */
public class AliSmsUtils {

//   private static RedisCache redisCache = SpringUtils.getBean(RedisCache.class);

    private static String accessKeyId = "LTAI5tRr6w3toNQqZjNdFpSS";

    private static String accessSecret = "xy0rz7fGbF3HLp1CGZEYdl0MSLU0e8";

    private static String regionId = "cn-beijing";

    private static String signName = "来转铺";
//    /**
//     * 短信认证 模板号
//     */
//    private static String templateCode = "SMS_244020106";
//    /**
//     * 报名通知模板号
//     */
//    private static String noticeCode = "SMS_268555156";
//
//    /**
//     * 证书领取模板号
//     */
//    private static String certCode = "SMS_268700151";

    /**
     * 来转铺
     */
    private static String cendCode = "SMS_479820216";


//    public static boolean sendCodeSms(String phoneNumber, String code) {
//        String paramString = "{\"code\":\"" + code + "\"}";
//        if (sendSms(signName, templateCode, paramString, phoneNumber)) {
//            redisCache.setCacheMapValue("code",phoneNumber,code);
//            return true;
//        }
//        return false;
//    }

//    public static boolean sendNoticeSms(String phoneNumber, String name, String activityName) {
//        String paramString = "{\"name\":\"" + name + "\",\"name1\":\"" + activityName + "\"}";
//        return sendSms(signName, noticeCode, paramString, phoneNumber);
//    }
//
//    public static boolean sendCertSms(String phoneNumber, String name, String activityName) {
//        String paramString = "{\"name\":\"" + name + "\",\"name1\":\"" + activityName + "\"}";
//        return sendSms(signName, certCode, paramString, phoneNumber);
//    }

    public static boolean sendSms(String signName, String templateCode, String paramString, String phoneNumber) {
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", regionId);
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", signName);
        request.putQueryParameter("TemplateCode", templateCode);
        request.putQueryParameter("TemplateParam", paramString);
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSONUtil.parseObj(response.getData());
            if (jsonObject.get("Code").equals("OK")) {
                return true;
            } else {
                if (jsonObject.get("Message").equals("触发分钟级流控Permits:1")) {
                    Assert.isTrue(false, "验证码发送太频繁了");
                }
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static boolean sendInfor(String phoneNumber, String name) {
        String paramString = "{\"code\":\"" + name + "\"}";
        return sendSms(signName, cendCode, paramString, phoneNumber);
    }

    public static void main(String[] args) {
//        String code = String.valueOf((int) ((Math.random() * 9 + 1) * 100000));
        System.out.println(sendInfor("15936554340", "13"));
    }
}
